#!/usr/bin/env python
# -*- coding: latin-1; py-indent-offset:4 -*-
################################################################################
# 
# This file is part of Bfplusplus
#
# Bfplusplus is a graphical interface to the Betfair Betting Exchange
# Copyright (C) 2010 Daniel Rodriguez (aka Daniel Rodriksson)
# Copyright (C) 2011 Sensible Odds Ltd.
#
# You can learn more and contact the author at:
#
#    http://code.google.com/p/bfplusplus/
#
# Bfplusplus is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Bfplusplus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Bfplusplus. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""Subclass of MainFrame, which is generated by wxFormBuilder."""
import wx

import MainGui

import threads.threadsbf as threadsbf

##################################################
# Implementing MainFrame

class MainFrame(MainGui.MainFrame):

    def __init__(self, parent):
        MainGui.MainFrame.__init__(self, parent)

        # Identify the version and learn if beta features have to be neables
        BetBuddyBetaString = {True: ' Beta ', False: ''}
        theApp = wx.GetApp()
        self.BetBuddyBeta = theApp.beta

        self.version = theApp.BetBuddyVersion

        self.BetBuddyStr = '%s %s%s' % (theApp.BetBuddyName, self.version, BetBuddyBetaString[self.BetBuddyBeta])
        self.SetTitle('%s' % self.BetBuddyStr)

        self.InitConstants()

        for initFunc in self.initlist:
            initFunc(self)
        # Refresh any changes to the interface

        self.myIcon = wx.Icon('./icons/bfplusplus.ico', wx.BITMAP_TYPE_ICO)
        self.SetIcon(self.myIcon)

        # self.myIcon = wx.Bitmap

        # self.Refresh()

        self.lPanelSize = self.m_panelMainLeft.GetBestSize()
        lwidth = self.lPanelSize.width
        lheight = self.lPanelSize.height
        self.m_panelMainLeft.SetSizeHints(lwidth, lheight, lwidth, lheight)
        # self.rPanelSize = self.m_panelMainRight.GetBestSize()
        # self.rPanelSize = self.m_scrolledWindowMainRight.GetBestSize()
        # Only the panel under the scrolled window reports a real "best size"
        self.rPanelSize = self.m_panelScrolled.GetBestSize()

        # print "rpanel best size is w: %d, h: %d" % (self.rPanelSize.width, self.rPanelSize.height)
        winsize = self.GetSize()
        height = winsize.height
        
        screenwidth, screenheight = wx.GetDisplaySize()
        if screenwidth <= 1024:
            # On less wider screens adjust the scrolled window size
            self.SetSize(wx.Size(screenwidth, winsize.height))
        else: # 1280 at least
            # Do nothing
            # +3 for the static line in between panels
            # +5 for each border ... 2 per panel ... 4 * 5
            width = self.lPanelSize.width + self.rPanelSize.width + 4 * 5 + 3
            self.SetSize(wx.Size(width, height))
            pass

        # Center ourselves to start with
        self.Center()

        w, h = self.m_panelMarketBets.GetBestSizeTuple()
        self.m_splitterMarketBets.SetSashPosition(5*h/3)

        # Apply all changes
        self.Refresh()

        # Disable vertical scrolling to avoid the grid reporting a large size
        # and forcing the scrolled window to have a large virtual viewport
        xunit, yunit = self.m_scrolledWindowMainRight.GetScrollPixelsPerUnit()
        self.m_scrolledWindowMainRight.SetScrollRate(xunit, 0)

        self.m_notebookModules.RemovePage(1)
        self.m_notebookModules.RemovePage(1)

        # Start the main working thread
        self.thMisc = threadsbf.ThreadMisc(self, self.OnMisc, proxydict=self.proxydict, version=self.version)

        # Check for update on start if requested
        self.CheckUpdateOnStart()


    def m_splitterMarketBetsOnIdle(self, event):
        # Done during init, which avoids a late change of the sash position
        # Having this function overrides the one generated by wxFormBuilder
        # w, h = self.m_panelMarketBets.GetBestSizeTuple()
        # self.m_splitterMarketBets.SetSashPosition(5*h/3)
        self.m_splitterMarketBets.Unbind(wx.EVT_IDLE)
